import * as api from '../../api/user';
import { goTo } from '../../utils';

export default {
  namespaced: true,
  state: {
    username: 'zhangsan',
  },
  mutations: {
    SET_USERNAME (state, newName) {
      state.username = newName;
    },
  },
  actions: {
    login (context, userInfo) {

      api.login({
        mobile: userInfo.name,
        password: userInfo.password,
      })
      .then((token) => {
        context.commit('SET_USERNAME', userInfo.name);
        localStorage.setItem('TOKEN', token);
        goTo('/');
      });
    },
  },
};
